package com.oracleoaec.dao.impl;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oracle.template.JDBCTemplate;
import com.oracle.template.PreparedStatementSetter;
import com.oracle.template.ResultSetHandler;
import com.oracleoaec.dao.BrandDao;
import com.oracleoaec.entity.Brand;

public class BrandDaoImpl extends JDBCTemplate implements BrandDao{

	private Brand brand;
	private List<Brand> list;
	//显示类型的名字
	@Override
	public Brand queryBrandNameById(final long id) {
		String sql="select * from t_brand where id=?";
		this.query(sql, new PreparedStatementSetter() {
			
			@Override
			public void setValues(PreparedStatement ps) throws SQLException {
				ps.setLong(1, id);
			}
		}, new ResultSetHandler() {
			
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				if(rs.next()){
					brand=new Brand(rs.getLong("id"),rs.getString("name"));
				}else{
					brand=null;
				}
			}
		});
		return brand;
	}
	@Override
	public List<Brand> queryAllBrand() {
		list=new ArrayList<Brand>();
		query("select * from t_brand",null,new ResultSetHandler() {
			@Override
			public void handleRs(ResultSet rs) throws SQLException {
				while(rs.next()){
					//System.out.println(rs.getLong("id")+"...."+rs.getString("name"));
					list.add(new Brand(rs.getLong("id"),rs.getString("name")));
				}
			}
		});
		return list;
	}
}
